package com.qztc.news_end_customer.controller;

import com.qztc.news_end_customer.config.GlobalConfig;
import com.qztc.news_end_dto.dto.Theme;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/thenews/theme")
public class ThemeControllerByRestTemplate extends GlobalConfig {


    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/get")
    public String get(){
        return restTemplate.getForObject(SEVICE_URL+"theme/get", String.class);
    }

    @PostMapping("/add")
    public String add(@RequestBody Theme theme){
        Map<String,Object> params = new HashMap<>();
        params.put("themename",theme.getThemename());
        return restTemplate.postForObject(SEVICE_URL+"theme/add", params, String.class);
    }

    @DeleteMapping("/deletebyid")
    public String delete(@RequestBody Theme theme){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Theme> httpEntity = new HttpEntity<>(theme,headers);
        ResponseEntity<String> result = restTemplate.exchange(SEVICE_URL+"theme/deletebyid", HttpMethod.DELETE,httpEntity,String.class);
        return result.getBody();
    }

    @PutMapping("/update")
    public String update(@RequestBody Theme theme){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Theme> httpEntity = new HttpEntity<>(theme,headers);
        ResponseEntity<String> result = restTemplate.exchange(SEVICE_URL+"theme/update", HttpMethod.PUT,httpEntity,String.class);
        return result.getBody();
    }

}
